using System.Data;
using System.Data.SqlClient;

namespace VMukti.DataAccess.VMuktiGrid
{
    public class ClsModuleDataService : ClsDataServiceBase
    {
        public ClsModuleDataService() : base() { }

        public ClsModuleDataService(IDbTransaction txn) : base(txn) { }

        public DataSet GetModules()
        {
            return ExecuteDataSet("select * from Module order by ModuleName", CommandType.Text, null);
        }

        public DataSet GetNonCollaborativeModules()
        {
            return ExecuteDataSet("select * from Module where NeedsAuthentication='true'and IsCollaborative='false' order by ModuleName", CommandType.Text, null);
        }

        public DataSet GetNonAuthenticatedModules()
        {
            return ExecuteDataSet("select * from Module where NeedsAuthentication='false' order by ModuleName", CommandType.Text, null);
        }

        public DataSet GetCollaborativeModules(int intRoleId)
        {
            //return ExecuteDataSet("select ModuleName,id,IsCollaborative from Module where IsCollaborative='true'", CommandType.Text, null);
            return ExecuteDataSet("spGCModules",CreateParameter("@pRoleId", SqlDbType.BigInt, intRoleId));
        }

        public DataSet GetOnlyCollaborativeModules()
        {
            return ExecuteDataSet("select * from Module where IsCollaborative='true' and NeedsAuthentication='true' order by ModuleName", CommandType.Text, null);
        }

        public int Add_Module(int ModuleId, string strModName, string strModVersion, string strDesc, string strAssFile, string strClassFile, string strZipFile, int intUserid, bool blnIsCollaborative, bool blnNeedAuth)
        {
            SqlCommand cmd;
            int id;
            ExecuteNonQuery(out cmd, "spAEModule",
            CreateParameter("@pID", SqlDbType.BigInt, ModuleId),
            CreateParameter("@pModuleName", SqlDbType.NVarChar, strModName),
            CreateParameter("@pModuleVersion", SqlDbType.NVarChar, strModVersion),
            CreateParameter("@pDescription", SqlDbType.NVarChar, strDesc),
            CreateParameter("@pAssemblyFile", SqlDbType.NVarChar, strAssFile),
            CreateParameter("@pClassName", SqlDbType.NVarChar, strClassFile),
            CreateParameter("@pZipFile", SqlDbType.NVarChar, strZipFile),
            CreateParameter("@pUserID", SqlDbType.BigInt, intUserid),
            CreateParameter("@pIsCollaborative", SqlDbType.Bit, blnIsCollaborative),
            CreateParameter("@pNeedsAuthentication", SqlDbType.Bit, blnNeedAuth),
            CreateParameter("@pReturnMaxID", SqlDbType.BigInt, ParameterDirection.Output));

            id = int.Parse(cmd.Parameters["@pReturnMaxID"].Value.ToString());
            cmd.Dispose();
            return id;
        }

        public DataSet Get_PodModule(int intModId)
        {
            return ExecuteDataSet("select * from Module where ID=" + intModId + "order by ModuleName" , CommandType.Text, null);
        }

        public bool Module_Exists(string strModName)
        {
            SqlCommand cmd;
            bool id;
            ExecuteNonQuery(out cmd, "spModuleExists",
            CreateParameter("@pModuleName", SqlDbType.NVarChar, strModName),
            CreateParameter("@Result", SqlDbType.Bit, ParameterDirection.Output));
            id = bool.Parse(cmd.Parameters["@Result"].Value.ToString());
            cmd.Dispose();
            return id;
        }
    }
}